package com.cwnu.interceptor;

import com.cwnu.utils.JWTUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @ProjectName: mall-cwnu
 * @Titile: TokenInterceptor
 * @Author: Lucky
 * @Description: token拦截器
 */
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //对所有的请求都要处理
        String token = request.getHeader("Authorization");
        if (Objects.isNull(token)) {
            throw  new RuntimeException("token不存在");
        }
        //含有token
        //判断是否合法
        try {
            JWTUtil.parseToken(token);
        } catch (Exception e) {
            throw new RuntimeException("token非法");
        }
        //合法
        return true;
    }
}
